package com.stable.action.login;

import org.apache.struts.action.*;
import javax.servlet.http.*;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;


public class LogoutAction extends Action {
    private static Log log = LogFactory.getLog(LogoutAction.class);
    
    /** Creates a new instance of LogoutAction */
    public LogoutAction() {
    }
    
    /**
     * meldet den aktuellen Benutzer aus den System ab
     */
    public ActionForward execute(ActionMapping actionMapping,
            ActionForm actionForm, HttpServletRequest request,
            HttpServletResponse httpServletResponse) throws Exception {
        HttpSession session = request.getSession();
        ActionMessages messages = new ActionMessages();
        
        Integer userId = (Integer) session.getAttribute("userId");
        if (userId == null) {
            messages.add("username", new ActionMessage("session.required"));
            saveMessages(request, messages);
            return actionMapping.findForward("fail");
        } else
            messages.clear();
        // User ist angemeldet
        session.removeAttribute("user");
        session.removeAttribute("userId");
        session.invalidate();
        
        return actionMapping.findForward("success");
    }
}
